package echat.future.cn.echat.message.weekrank;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.message.MessageApi;
import echat.future.cn.echat.message.bean.RankListBean;
import echat.future.cn.echat.message.bean.WeekGroupRankBean;
import echat.future.cn.echat.message.bean.WeekRankBean;
import echat.future.cn.echat.message.rank.bean.RankGroupBean;
import echat.future.cn.echat.message.weekrank.adapter.WeekRankAdapter;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.weight.CircleImageView;
import lumingweihua.future.cn.baselibgxh.base.BaseActivity;

/**
 * Created by JX on 2018/8/22.
 */

public class RankWeekActivity extends BaseActivity {

    private static final String REFRESH_MIN = "排行榜每1分钟刷新一次\\n本活动最终解释权归官方所有";
    private static final String GUANFANG = "本活动最终解释权归官方所有";
    private static final int XIANNV = 2; //仙女
    private static final int TUHAO = 1;// 土豪
    private static final int GUIZE = 3; //规则

    private static final String WEEK_ID = "week_id";

    //仙女，土豪两个按钮
    private LinearLayout ll_btn;
    private TextView btn_xiannv;
    private TextView btn_tuhao;

    private RecyclerView rv;
    private TextView tv_bot;

    private TextView tv_time;
    private LinearLayout rl1;
    private RelativeLayout rl2;
    private RelativeLayout rl3;
    private CircleImageView iv_head1;
    private CircleImageView iv_head2;
    private CircleImageView iv_head3;
    private TextView tv_name1;
    private TextView tv_name2;
    private TextView tv_name3;
    private TextView tv_count1;
    private TextView tv_count2;
    private TextView tv_count3;

    private LinearLayout ll_rank;
    private LinearLayout ll_content;
    private TextView tv_content;

    private WeekRankAdapter mAdapter;
    private List<WeekRankBean> mData;

    private WeekGroupRankBean tuhaoBean;
    private WeekGroupRankBean xiannvBean;

    private WeekRankBean weekRankBean1;
    private WeekRankBean weekRankBean2;
    private WeekRankBean weekRankBean3;

    private int type = XIANNV;//当前点击类型
    private int week_id;

    public static Intent getIntent(Context context, int week_id) {
        Intent intent = new Intent(context, RankWeekActivity.class);
        intent.putExtra(WEEK_ID, week_id);
        return intent;
    }

    @Override
    protected void initView() {
        setContentView(R.layout.rank_week_activity);
        StatusBarUtils.hideStatus(this);

        week_id = getIntent().getIntExtra(WEEK_ID, 0);

        rv = findViewById(R.id.rv);
        ll_btn = findViewById(R.id.ll_btn);
        btn_xiannv = findViewById(R.id.btn_xiannv);
        btn_tuhao = findViewById(R.id.btn_tuhao);
        tv_time = findViewById(R.id.tv_time);

        tv_time = findViewById(R.id.tv_time);
        rl1 = findViewById(R.id.rl1);
        rl2 = findViewById(R.id.rl2);
        rl3 = findViewById(R.id.rl3);
        iv_head1 = findViewById(R.id.iv_head1);
        iv_head2 = findViewById(R.id.iv_head2);
        iv_head3 = findViewById(R.id.iv_head3);
        tv_name1 = findViewById(R.id.tv_name1);
        tv_name2 = findViewById(R.id.tv_name2);
        tv_name3 = findViewById(R.id.tv_name3);
        tv_count1 = findViewById(R.id.tv_count1);
        tv_count2 = findViewById(R.id.tv_count2);
        tv_count3 = findViewById(R.id.tv_count3);

        ll_rank = findViewById(R.id.ll_rank);
        ll_content = findViewById(R.id.ll_content);
        tv_content = findViewById(R.id.tv_content);

        setOnClickListener();

        mData = new ArrayList<>();
        mAdapter = new WeekRankAdapter(R.layout.rank_week_rv_item, mData);
        mAdapter.bindToRecyclerView(rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
    }

    private void setOnClickListener() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_wangqi).setOnClickListener(this);
        findViewById(R.id.iv_guize).setOnClickListener(this);
        findViewById(R.id.btn_xiannv).setOnClickListener(this);
        findViewById(R.id.btn_tuhao).setOnClickListener(this);
    }

    @Override
    protected void initData() {

        if (type == XIANNV && xiannvBean != null) {
            setData(xiannvBean);
            return;
        }

        if (type == TUHAO && tuhaoBean != null) {
            setData(tuhaoBean);
            return;
        }

        new HttpUtilsRequst<WeekGroupRankBean>(this, HttpUtilsRequst.retrofitClient.create(MessageApi.class)
                .week_rank_info(type, week_id))
                .setmOnRequstSuccessListener(new HttpUtilsRequst.OnRequstSuccessListener<WeekGroupRankBean>() {
                    @Override
                    public void onSuccss(WeekGroupRankBean weekGroupRankBean) {
                        if (type == XIANNV) {
                            xiannvBean = weekGroupRankBean;
                        }else {
                            tuhaoBean = weekGroupRankBean;
                        }

                        if(!TextUtils.isEmpty(weekGroupRankBean.getContent()))
                            tv_content.setText(Html.fromHtml(weekGroupRankBean.getContent()));

                        setData(weekGroupRankBean);
                    }
                })
                .setmOnRequstFaileListener(new HttpUtilsRequst.OnRequstFaileListener() {
                    @Override
                    public void onFaile(int code, String msg) {

                    }
                })
                .start();
    }

    @Override
    protected void processClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_wangqi://往期活动
                startActivity(new Intent(this, RankListActivity.class));
                break;
            case R.id.iv_guize://规则奖励
                clickGuize();
                break;
            case R.id.btn_xiannv://仙女
                clickXiannv();
                break;
            case R.id.btn_tuhao://土豪
                clickTuhao();
                break;
        }
    }

    //规则点击事件
    private void clickGuize() {
        if (type == GUIZE) return;
        type = GUIZE;
        ll_content.setVisibility(View.VISIBLE);
        ll_rank.setVisibility(View.GONE);
    }

    //仙女点击事件
    private void clickXiannv() {
        if (type == XIANNV) return;
        type = XIANNV;
        setXiannvAndTuhao();

        initData();
    }

    //土豪点击事件
    private void clickTuhao() {
        if (type == TUHAO) return;
        type = TUHAO;
        setXiannvAndTuhao();

        initData();
    }

    private void setXiannvAndTuhao() {
        ll_content.setVisibility(View.GONE);
        ll_rank.setVisibility(View.VISIBLE);

        if (type == XIANNV) {
            btn_xiannv.setBackgroundResource(R.drawable.rank_select);
            btn_tuhao.setBackgroundResource(R.drawable.rank_normal);
        } else {
            btn_xiannv.setBackgroundResource(R.drawable.rank_normal);
            btn_tuhao.setBackgroundResource(R.drawable.rank_select);
        }
    }

    private void setData(WeekGroupRankBean data) {
        mData.clear();
        mData.addAll(converData(data.getList()));
        mAdapter.setType(type);
        mAdapter.notifyDataSetChanged();

        tv_time.setText("活动时间 " + data.getStartTime() + "-"+data.getEndTime());

        initThree();
    }

    private List<WeekRankBean> converData(List<WeekRankBean> data) {
        List<WeekRankBean> list = new ArrayList<>();
        weekRankBean1 = null;
        weekRankBean2 = null;
        weekRankBean3 = null;
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                weekRankBean1 = data.get(i);
            } else if (i == 1) {
                weekRankBean2 = data.get(i);
            } else if (i == 2) {
                weekRankBean3 = data.get(i);
            } else {
                list.add(data.get(i));
            }
        }
        return list;
    }

    private void initThree() {
        if (weekRankBean1 == null) {
            rl1.setVisibility(View.INVISIBLE);
        } else {
            rl1.setVisibility(View.VISIBLE);
            setThreeData(weekRankBean1, iv_head1, tv_name1, tv_count1);
        }

        if (weekRankBean2 == null) {
            rl2.setVisibility(View.INVISIBLE);
        } else {
            rl2.setVisibility(View.VISIBLE);
            setThreeData(weekRankBean2, iv_head2, tv_name2, tv_count2);
        }

        if (weekRankBean3 == null) {
            rl3.setVisibility(View.INVISIBLE);
        } else {
            rl3.setVisibility(View.VISIBLE);
            setThreeData(weekRankBean3, iv_head3, tv_name3, tv_count3);
        }
    }

    private void setThreeData(WeekRankBean bean, CircleImageView iv_head, TextView tv_name, TextView tv_count) {
        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(R.mipmap.ic_launcher);
        Glide.with(this).load(bean.getAvatar()).apply(options).into(iv_head);
        String text = type == XIANNV ? "收获" + bean.getSum() + "个" : "送出" + bean.getSum() + "个";
        tv_count.setText(getSpannable(text));
        tv_name.setText(bean.getUsername());
    }

    private SpannableStringBuilder getSpannable(String text) {
        SpannableStringBuilder spannable = new SpannableStringBuilder(text);
        spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#F35043")),2,text.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

}